package com.yl.chainStore.control.user

import com.alibaba.fastjson.JSONObject
import com.baomidou.mybatisplus.core.toolkit.IdWorker
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.yl.chainStore.control.BaseCtrl
import com.yl.chainStore.services.IWebCompanyUserService
import com.yl.common.ErrorCode
import com.yl.common.Flag
import com.yl.common.UserType
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnError
import com.yl.common.control.returnSuccess
import com.yl.common.getContext
import com.yl.users.entity.CompanyUser
import com.yl.users.pojo.vo.CompanyUserSelectVo
import com.yl.users.services.ICompanyDeptService
import com.yl.users.services.ICompanyUserService
import com.yl.users.services.ISysCompanyService
import org.springframework.transaction.annotation.Transactional
import org.springframework.transaction.interceptor.TransactionAspectSupport
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("company/dept/user")
open class CompanyDeptUserController(
	private val iCompanyUserService: ICompanyUserService,
	private val iSysCompanyService: ISysCompanyService,
	private val iCompanyDeptService: ICompanyDeptService,
	private val iWebCompanyUserService: IWebCompanyUserService
):BaseCtrl() {

	/**
	 * 用户分页
	 */
	@PostMapping("page")
//	@PermsCode("company:user:user:page")
	@Transactional
	open fun page(@RequestBody vo: CompanyUserSelectVo): ResponseBody {
		val page = Page<CompanyUser>(vo.current, vo.size)
		if(vo.deptId == null){
			vo.deptId = getContext().subCompanyId
		}
		val resList = iCompanyDeptService.getUserByDeptId(page, vo, vo.deptId!!)
		for(user in resList.records){
			user.roleIds = iCompanyUserService.getUserSubCompanyRoleIds(user.id, user.subCompanyId!!)
		}
		return returnSuccess(resList)
	}

	/**
	 * 添加用户
	 */
	@PostMapping("addUser")
	@Transactional(rollbackFor = [Exception::class])
	open fun addUser(@RequestBody user: CompanyUser):ResponseBody{
		user.userCode = IdWorker.get32UUID()
		user.flag = Flag.NEW_ADD
		user.userType = UserType.Company
		user.companyId = getContext().companyId
		val addUser = iSysCompanyService.addUser(user)
		if (addUser.bodyIsError()) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
			return addUser
		}
		if(user.deptId == null){
			user.deptId = getContext().subCompanyId
		}
		val bindUserToDept = iCompanyDeptService.bindUserToDept(user.deptId!!, user.id)
		if (bindUserToDept.bodyIsError()) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
			return bindUserToDept
		}
		return returnSuccess()
	}

	/**
	 * 修改用户基本信息
	 */
	@PostMapping("updateUser")
	@Transactional(rollbackFor = [Exception::class])
	open fun updateUser(@RequestBody user: CompanyUser):ResponseBody{
		val updateUser = iCompanyUserService.updateById(user)
		if (updateUser.code != ErrorCode.SUCCESS.code) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
			return updateUser
		}
		return returnSuccess()
	}

	/**
	 * 用户绑定部门（再绑定分院）
	 */
	@PostMapping("bindUserToDept")
	@Transactional(rollbackFor = [Exception::class])
	open fun userBindDeptId(@RequestBody body: JSONObject):ResponseBody{
		val userId = body.getLong("userId") ?: return returnError(ErrorCode.PARAM_EMPTY, "请选择用户")
		val deptId = body.getLong("deptId") ?: return returnError(ErrorCode.PARAM_EMPTY, "请选择部门")
		val bindUserToDept = iCompanyDeptService.bindUserToDept(deptId, userId)
		if(bindUserToDept.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()

		}
		return bindUserToDept
	}

	/**
	 * 解绑
	 */
	@PostMapping("unBindUserToDept")
	@Transactional(rollbackFor = [Exception::class])
	open fun unBindUserToDept(@RequestBody body: JSONObject): ResponseBody{
		val userId = body.getLong("userId") ?: return returnError(ErrorCode.PARAM_EMPTY, "请选择用户")
		val deptId = body.getLong("deptId") ?: return returnError(ErrorCode.PARAM_EMPTY, "请选择部门")
		val unBindUserDept = iCompanyDeptService.unBindUserDept(deptId, userId)
		if(unBindUserDept.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return unBindUserDept
	}

}
